package org.grayrabbit.cloud.utils;

import java.io.ByteArrayOutputStream;
import java.util.List;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class PropertyBuilder {	
		
	public static String CommProperties(Property[] Pros){
		
		StringBuffer sb = new StringBuffer();
		
		for(int i=0;i<Pros.length;i++){
			Property pro = Pros[i];
			
			sb.append(" ");
			sb.append("-").append(pro.getKey()).append(":").append(pro.getValue());			
			
		}
		
		return sb.toString();
		
	}
	
	public static String CommProperties(List<Property> Pros){
		
		StringBuffer sb = new StringBuffer();
		
		for(int i=0;i<Pros.size();i++){
			Property pro = Pros.get(i);
			
			sb.append(" ");
			sb.append("-").append(pro.getKey()).append(":").append(pro.getValue());			
			
		}
		
		return sb.toString();
		
	}
	
	public static String xmlProperties(String name,Property[] Pros)
	throws ParserConfigurationException,TransformerException{
				
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		Document document = builder.newDocument();
		
		Element root = document.createElement("argument");
		root.setAttribute("name",name);
		if(Pros!=null){
			for(int i=0;i<Pros.length;i++){
				Property pro = Pros[i];
				Element client = document.createElement("properties");
				client.setAttribute("name",pro.getKey());
				client.setAttribute("value",String.valueOf(pro.getValue()));
				root.appendChild(client);
			}		
		}
		document.appendChild(root);
		
		ByteArrayOutputStream out = new ByteArrayOutputStream(512);
		DOMSource source = new DOMSource(document);
		StreamResult result = new StreamResult(out);
		
		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer = tFactory.newTransformer();
		
		Properties properties = transformer.getOutputProperties();
		properties.setProperty(OutputKeys.ENCODING,"GB2312");
		transformer.setOutputProperties(properties);
		
		transformer.transform(source, result);
		return out.toString();
		
	}	
	
	public static String xmlProperties(String name,List<Property> Pros)
	throws ParserConfigurationException,TransformerException{
		
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		Document document = builder.newDocument();
		
		Element root = document.createElement("argument");
		root.setAttribute("name",name);		
		for(int i=0;i<Pros.size();i++){
			Property pro = Pros.get(i);
			Element client = document.createElement("properties");
			client.setAttribute("name",pro.getKey());
			client.setAttribute("value",String.valueOf(pro.getValue()));
			root.appendChild(client);
		}		
		document.appendChild(root);
		
		ByteArrayOutputStream out = new ByteArrayOutputStream(512);		
		DOMSource source = new DOMSource(document);
		StreamResult result = new StreamResult(out);
		
		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer = tFactory.newTransformer();
		
		Properties properties = transformer.getOutputProperties();
		properties.setProperty(OutputKeys.ENCODING,"GB2312");
		transformer.setOutputProperties(properties);
		
		transformer.transform(source, result);
		return out.toString();
		
	}	
	
	public static String[] GetKeys(Property[] Pros){
		
		String keys[] = new String[Pros.length];
		
		for(int i=0;i<Pros.length;i++){
			Property pro = Pros[i];					
			keys[i] = pro.getKey().trim();
		}
		
		return keys;		
	}	
				
}
